package manager.logdata;

import java.util.ArrayList;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import manager.data.RawDataObject;

import org.hibernate.SessionFactory;
import org.hibernate.classic.Session;
import store.hibernate.LogData;

public class LogDataObject {
	
	private SessionFactory factory;

	public LogDataObject(){
		try { 
			InitialContext ctx = new InitialContext(); 
			factory = (SessionFactory)ctx.lookup("java:/hibernate/SessionFactory"); 
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public boolean insertBlock(ArrayList<RawDataObject> ml) {
		Session session = factory.openSession();
	     session.beginTransaction();
	     int size = ml.size();
	     for(int i=0;i<size;i++){
	    	 RawDataObject rdo = ml.get(0);
	    	 ml.remove(0);

	    	 LogData eq = new LogData(
	    			 rdo.getDirection(), 
	    			 rdo.getSize(), 
	    			 rdo.getTimestamp(), 
	    			 rdo.getSeqNumber(), 
	    			 rdo.getSession(),
	    			 rdo.getUsername(),
	    			 rdo.getOperation(),
	    			 rdo.getPassword(),
	    			 rdo.getWSName(),
	    			 rdo.getWSNamespace(),
	    			 rdo.getWSDLURL()
	    	 );
	    	 if(rdo.getParams()!=null)
	    		 eq.setParametriByte(rdo.getParams());
	    	 session.save(eq);
	    	 System.out.println("inserisco in log "+size+ " record...");

	     }
	     session.getTransaction().commit();
	     if (session.isOpen()) 
	    	 session.close(); 
	     return true;
	}
	
}
